<?php
require_once 'config.php';
require_once 'db.php';
session_start();

// 检查是否已登录
if (!isset($_SESSION['username'])) {
    echo json_encode(['success' => false, 'message' => '未登录']);
    exit;
}

// 检查参数
if (!isset($_POST['id']) || !isset($_POST['remark'])) {
    echo json_encode(['success' => false, 'message' => '参数错误']);
    exit;
}

$id = intval($_POST['id']);
$remark = trim($_POST['remark']);

// 验证备注长度
if (mb_strlen($remark, 'UTF-8') > 6) {
    echo json_encode(['success' => false, 'message' => '备注不能超过6个汉字']);
    exit;
}

// 连接数据库
$conn = getDBConnection();

// 更新备注
$stmt = $conn->prepare("UPDATE tracking_history SET remark = ? WHERE id = ? AND name = ?");
$stmt->bind_param("sis", $remark, $id, $_SESSION['username']);
$result = $stmt->execute();

if ($result) {
    echo json_encode(['success' => true]);
} else {
    echo json_encode(['success' => false, 'message' => '更新失败', 'error' => $stmt->error]);
}

$stmt->close();
$conn->close();
?> 